﻿using System;
using System.Collections.Generic;
using System.Web;

namespace MikeKappel.Com.CS
{
    public static partial class Helper
    {
        private const String TRANSFER_DATA = "TransferData";
        private const String REDIRECTED_FROM = "RedirectedFrom";

        /// <summary>
        /// Secure way to transfer data between pages
        /// </summary>
        public static Object TranferData
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[TRANSFER_DATA] == null)
                {
                    return null;
                }
                else if (((MikeKappel.Com.CS.Model.TimeSensitive<Object>)System.Web.HttpContext.Current.Session[TRANSFER_DATA]).Time < DateTime.Now)
                {
                    System.Web.HttpContext.Current.Session[TRANSFER_DATA] = null;
                    return null;
                }
                else
                {
                    return ((MikeKappel.Com.CS.Model.TimeSensitive<Object>)System.Web.HttpContext.Current.Session[TRANSFER_DATA]).Value;
                }
            }
            set
            {
                //This should be a very small amout of time (20 Seconds)
                //This is only used to pass infomation from on page to the next.
                TimeSpan SecondsVaidFor = new TimeSpan(0, 0, 20);
                System.Web.HttpContext.Current.Session[TRANSFER_DATA] = new MikeKappel.Com.CS.Model.TimeSensitive<Object>(SecondsVaidFor, value);
            }
        }
        /// <summary>
        /// The last page the user was redirected from.
        /// </summary>
        public static Uri RedirectedFrom
        {
            get{
                if (System.Web.HttpContext.Current.Session[REDIRECTED_FROM] == null)
                {
                    return null;
                }
                else if (((MikeKappel.Com.CS.Model.TimeSensitive<Uri>)System.Web.HttpContext.Current.Session[REDIRECTED_FROM]).Time < DateTime.Now)
                {
                    System.Web.HttpContext.Current.Session[REDIRECTED_FROM] = null;
                    return null;
                }
                else
                {
                    return ((MikeKappel.Com.CS.Model.TimeSensitive<Uri>)System.Web.HttpContext.Current.Session[REDIRECTED_FROM]).Value;
                }
            }
            set
            {
                if (!value.LocalPath.Contains(".asmx"))
                {
                    //This should be a very small amout of time (20 Seconds)
                    //This is only used to pass infomation from on page to the next.
                    TimeSpan SecondsVaidFor = new TimeSpan(0, 0, 20);
                    System.Web.HttpContext.Current.Session[REDIRECTED_FROM] = new MikeKappel.Com.CS.Model.TimeSensitive<Uri>(SecondsVaidFor, value);
                }
            }
        }
        /// <summary>
        /// Standard Response.Redirect
        /// "302 Object moved"
        /// </summary>
        /// <param name="Url">Url the user will be redirected to.</param>
        public static void GoTo(String url)
        {
            GoTo(url, true);
        }
        /// <summary>
        /// Standard Response.Redirect
        /// "302 Object moved"
        /// </summary>
        /// <param name="Url">Url the user will be redirected to.</param>
        /// <param name="tranferData">Data to Securely Transfer</param>
        /// <param name="endResponse">Indicates whether execution of the current page should terminate.</param>
        public static void GoTo(String url, Object tranferData, Boolean endResponse)
        {
            TranferData = tranferData;
            GoTo(url, endResponse);
        }
        /// <summary>
        /// Standard Response.Redirect
        /// "302 Object moved"
        /// </summary>
        /// <param name="Url">Url the user will be redirected to.</param>
        /// <param name="endResponse">Indicates whether execution of the current page should terminate.</param>
        public static void GoTo(String url, Boolean endResponse)
        {
            if (!HttpContext.Current.Response.IsRequestBeingRedirected)
            {
                RedirectedFrom = HttpContext.Current.Request.Url;
                HttpContext.Current.Response.Redirect(url, endResponse);
            }
        }

    }
}